<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="theme-color" content="#0078E7"><meta name="author" content="爱肖彤真是太好了"><meta name="copyright" content="爱肖彤真是太好了"><meta name="generator" content="Hexo 5.1.1"><meta name="theme" content="hexo-theme-yun"><title>BeautifulSoup模块的使用 | 工藤旧二の博客</title><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@900&amp;display=swap" media="none" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/star-markdown-css@0.1.19/dist/yun/yun-markdown.min.css"><script src="//at.alicdn.com/t/font_1140697_stqaphw3j4.js" async></script><script src="https://cdn.jsdelivr.net/npm/scrollreveal/dist/scrollreveal.min.js" defer></script><script>document.addEventListener("DOMContentLoaded", () => {
  [".post-card",".post-content img"].forEach((target)=> {
    ScrollReveal().reveal(target);
  })
});
</script><link rel="shortcut icon" type="image/svg+xml" href="/task/yun.ico"><link rel="mask-icon" href="/task/yun.ico" color="#0078E7"><link rel="alternate icon" href="/yun.ico"><link rel="preload" href="/task/css/hexo-theme-yun.css" as="style"><link rel="preload" href="/task/js/utils.js" as="script"><link rel="preload" href="/task/js/hexo-theme-yun.js" as="script"><link rel="prefetch" href="/task/js/sidebar.js" as="script"><link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin><link rel="stylesheet" href="/task/css/hexo-theme-yun.css"><link rel="alternate" href="/task/atom.xml" title="工藤旧二の博客" type="application/atom+xml"><script id="yun-config">
    const Yun = window.Yun || {};
    window.CONFIG = {"root":"/task/","title":"工藤旧二の博客","version":"0.9.7","anonymous_image":"https://cdn.jsdelivr.net/gh/YunYouJun/cdn/img/avatar/none.jpg","say":{"api":"https://v1.hitokoto.cn","hitokoto":true},"local_search":{"path":"/task/search.xml"},"fireworks":{"colors":["102, 167, 221","62, 131, 225","33, 78, 194"]}};
  </script><script>(function(){
  var bp = document.createElement('script');
  var curProtocol = window.location.protocol.split(':')[0];
  if (curProtocol === 'https') {
    bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
  }
  else {
    bp.src = 'http://push.zhanzhang.baidu.com/push.js';
  }
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(bp, s);
})();</script><meta name="description" content="BeautifulSoup模块的使用1 参数介绍html_doc &#x3D; &quot;&quot;&quot; &lt;html&gt;&lt;head&gt;&lt;title&gt;The Dormouse&#39;s story&lt;&#x2F;title&gt;&lt;&#x2F;head&gt; &lt;body&gt; &lt;p id&#x3D;&quot;my_p&quot; class&#x3D;&quot;title&quot;&gt;hello&lt;b id&#x3D;&quot;bbb&quot; class&#x3D;&quot;boldest&quot;&gt;The Dormouse&#39;s">
<meta property="og:type" content="article">
<meta property="og:title" content="BeautifulSoup模块的使用">
<meta property="og:url" content="http://huang_zhao.gitee.io/task/2020/04/21/python/%E7%88%AC%E8%99%AB/BeautifulSoup%E6%A8%A1%E5%9D%97%E7%9A%84%E4%BD%BF%E7%94%A8/index.html">
<meta property="og:site_name" content="工藤旧二の博客">
<meta property="og:description" content="BeautifulSoup模块的使用1 参数介绍html_doc &#x3D; &quot;&quot;&quot; &lt;html&gt;&lt;head&gt;&lt;title&gt;The Dormouse&#39;s story&lt;&#x2F;title&gt;&lt;&#x2F;head&gt; &lt;body&gt; &lt;p id&#x3D;&quot;my_p&quot; class&#x3D;&quot;title&quot;&gt;hello&lt;b id&#x3D;&quot;bbb&quot; class&#x3D;&quot;boldest&quot;&gt;The Dormouse&#39;s">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-04-21T06:10:11.000Z">
<meta property="article:modified_time" content="2020-09-04T11:23:29.000Z">
<meta property="article:author" content="爱肖彤真是太好了">
<meta property="article:tag" content="爬虫">
<meta property="article:tag" content="python模块">
<meta name="twitter:card" content="summary"><script src="/task/js/ui/mode.js"></script><link rel="stylesheet" href="/task/css/prism.css" type="text/css"></head><body><script defer src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script defer src="/task/js/ui/fireworks.js"></script><canvas class="fireworks"></canvas><div class="container"><a class="sidebar-toggle hty-icon-button" id="menu-btn"><div class="hamburger hamburger--spin" type="button"><span class="hamburger-box"><span class="hamburger-inner"></span></span></div></a><div class="sidebar-toggle sidebar-overlay"></div><aside class="sidebar"><script src="/task/js/sidebar.js"></script><ul class="sidebar-nav"><li class="sidebar-nav-item sidebar-nav-toc hty-icon-button sidebar-nav-active" data-target="post-toc-wrap" title="文章目录"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-list-ordered"></use></svg></li><li class="sidebar-nav-item sidebar-nav-overview hty-icon-button" data-target="site-overview-wrap" title="站点概览"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-passport-line"></use></svg></li></ul><div class="sidebar-panel" id="site-overview-wrap"><div class="site-info fix-top"><a class="site-author-avatar" href="/task/about/" title="爱肖彤真是太好了"><img width="96" loading="lazy" src="/task/Yun.png" alt="爱肖彤真是太好了"></a><div class="site-author-name"><a href="/task/about/">爱肖彤真是太好了</a></div><a class="site-name" href="/task/about/site.html">工藤旧二の博客</a><sub class="site-subtitle"></sub><div class="site-desciption"></div></div><nav class="site-state"><a class="site-state-item hty-icon-button icon-home" href="/task/" title="首页"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-home-4-line"></use></svg></span></a><div class="site-state-item"><a href="/task/archives/" title="归档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-archive-line"></use></svg></span><span class="site-state-item-count">57</span></a></div><div class="site-state-item"><a href="/task/categories/" title="分类"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-2-line"></use></svg></span><span class="site-state-item-count">2</span></a></div><div class="site-state-item"><a href="/task/tags/" title="标签"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="site-state-item-count">50</span></a></div><a class="site-state-item hty-icon-button" target="_blank" rel="noopener" href="https://yun.yunyoujun.cn" title="文档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-settings-line"></use></svg></span></a></nav><hr style="margin-bottom:0.5rem"><div class="links-of-author"><a class="links-of-author-item hty-icon-button" rel="noopener" title="RSS" target="_blank" style="color:orange"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-rss-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="QQ" target="_blank" style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="GitHub" target="_blank" style="color:#6e5494"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-github-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="微博" target="_blank" style="color:#E6162D"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-weibo-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="豆瓣" target="_blank" style="color:#007722"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-douban-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://music.163.com/#/user/home?id=552858287" title="网易云音乐" target="_blank" style="color:#C20C0C"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-netease-cloud-music-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="知乎" target="_blank" style="color:#0084FF"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-zhihu-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://space.bilibili.com/15081363" title="哔哩哔哩" target="_blank" style="color:#FF8EB3"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-bilibili-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Twitter" target="_blank" style="color:#1da1f2"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-twitter-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Telegram Channel" target="_blank" style="color:#0088CC"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-telegram-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="E-Mail" target="_blank" style="color:#8E71C1"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-mail-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Travelling" target="_blank" style="color:var(--hty-text-color)"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-send-plane-2-line"></use></svg></a></div><hr style="margin:0.5rem 1rem"><div class="links"><a class="links-item hty-icon-button" href="/task/links/" title="我的小伙伴们" style="color:dodgerblue"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-genderless-line"></use></svg></a></div><br><a class="links-item hty-icon-button" id="toggle-mode-btn" href="javascript:;" title="Mode" style="color: #f1cb64"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-contrast-2-line"></use></svg></a></div><div class="sidebar-panel sidebar-panel-active" id="post-toc-wrap"><div class="post-toc"><div class="post-toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#BeautifulSoup%E6%A8%A1%E5%9D%97%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-text">BeautifulSoup模块的使用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E5%8F%82%E6%95%B0%E4%BB%8B%E7%BB%8D"><span class="toc-text">1 参数介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="toc-text">2 基本使用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E6%90%9C%E7%B4%A2%E6%96%87%E6%A1%A3%E6%A0%91"><span class="toc-text">3 搜索文档树</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#3-1-%E4%BA%94%E7%A7%8D%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="toc-text">3.1 五种过滤器</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-2-find-all"><span class="toc-text">3.2 find_all()</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-3-css%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-text">3.3 css选择器</span></a></li></ol></li></ol></li></ol></li></ol></div></div></div></aside><main class="sidebar-translate" id="content"><div id="post"><article class="post-block" itemscope itemtype="https://schema.org/Article"><link itemprop="mainEntityOfPage" href="http://huang_zhao.gitee.io/task/task/2020/04/21/python/%E7%88%AC%E8%99%AB/BeautifulSoup%E6%A8%A1%E5%9D%97%E7%9A%84%E4%BD%BF%E7%94%A8/"><span hidden itemprop="author" itemscope itemtype="https://schema.org/Person"><meta itemprop="name" content="爱肖彤真是太好了"><meta itemprop="description"></span><span hidden itemprop="publisher" itemscope itemtype="https://schema.org/Organization"><meta itemprop="name" content="工藤旧二の博客"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">BeautifulSoup模块的使用</h1><div class="post-meta"><div class="post-time" style="display:block"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-line"></use></svg></span> <time title="创建时间：2020-04-21 14:10:11" itemprop="dateCreated datePublished" datetime="2020-04-21T14:10:11+08:00">2020-04-21</time><span class="post-meta-divider">-</span><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-2-line"></use></svg></span> <time title="修改时间：2020-09-04 19:23:29" itemprop="dateModified" datetime="2020-09-04T19:23:29+08:00">2020-09-04</time></div><span class="post-count"><span class="post-symbolcount"><span class="post-meta-item-icon" title="本文字数"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-file-word-line"></use></svg></span> <span title="本文字数">826</span><span class="post-meta-divider">-</span><span class="post-meta-item-icon" title="阅读时长"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-timer-line"></use></svg></span> <span title="阅读时长">3m</span></span></span><span class="post-busuanzi"><span class="post-meta-divider">-</span><span class="post-meta-item-icon" title="阅读次数"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-eye-line"></use></svg> <span id="busuanzi_value_page_pv"></span></span></span><div class="post-classify"><span class="post-category"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-line"></use></svg></span> <span itemprop="about" itemscope itemtype="https://schema.org/Thing"><a class="category" href="/task/categories/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/" style="--text-color:var(--hty-text-color)" itemprop="url" rel="index"><span itemprop="text">技术分享</span></a></span></span><span class="post-tag"><span class="post-meta-divider">-</span><a class="tag" href="/task/tags/%E7%88%AC%E8%99%AB/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">爬虫</span></a><a class="tag" href="/task/tags/python%E6%A8%A1%E5%9D%97/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">python模块</span></a></span></div></div></header><section class="post-body" itemprop="articleBody"><div class="post-content markdown-body" style="--smc-primary:#0078E7;"><h2 id="BeautifulSoup模块的使用"><a href="#BeautifulSoup模块的使用" class="headerlink" title="BeautifulSoup模块的使用"></a>BeautifulSoup模块的使用</h2><h4 id="1-参数介绍"><a href="#1-参数介绍" class="headerlink" title="1 参数介绍"></a>1 参数介绍</h4><pre class=" language-python"><code class="language-python">html_doc <span class="token operator">=</span> <span class="token triple-quoted-string string">"""
&lt;html>&lt;head>&lt;title>The Dormouse's story&lt;/title>&lt;/head>
&lt;body>
&lt;p id="my_p" class="title">hello&lt;b id="bbb" class="boldest">The Dormouse's story&lt;/b>
&lt;/p>

&lt;p class="story">Once upon a time there were three little sisters; and their names were
&lt;a href="http://example.com/elsie" class="sister" id="link1">Elsie&lt;/a>&lt;a href="http://example.com/lacie" class="sister" id="link2">Lacie&lt;/a> and
&lt;a href="http://example.com/tillie" class="sister" id="link3">Tillie&lt;/a>;
and they lived at the bottom of a well.&lt;/p>

&lt;p class="story">...&lt;/p>
"""</span>
soup <span class="token operator">=</span> BeautifulSoup<span class="token punctuation">(</span>html_doc<span class="token punctuation">,</span><span class="token string">'lxml'</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 第一个参数是要解析的html页面，通常是我们通过请求得到的页面数据</span>
<span class="token comment" spellcheck="true"># 第二个参数是解析的解析器选择</span>
<span class="token comment" spellcheck="true"># python内置的html.parser</span>
<span class="token comment" spellcheck="true"># 其他解释器：lxml（通常使用），xml，html5lib</span></code></pre>
<p>文档：<a target="_blank" rel="noopener" href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html">https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html</a></p>
<h4 id="2-基本使用"><a href="#2-基本使用" class="headerlink" title="2 基本使用"></a>2 基本使用</h4><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true"># 1 标签查找</span>
soup <span class="token operator">=</span> BeautifulSoup<span class="token punctuation">(</span>html_doc<span class="token punctuation">,</span><span class="token string">'lxml'</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true"># 如果有多个只会找到第一个</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>p
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>p<span class="token punctuation">.</span>b

<span class="token comment" spellcheck="true"># 2 标签属性</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>p<span class="token punctuation">.</span>b<span class="token punctuation">.</span>attrs
<span class="token punctuation">{</span><span class="token string">'id'</span><span class="token punctuation">:</span> <span class="token string">'bbb'</span><span class="token punctuation">,</span> <span class="token string">'class'</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">'boldest'</span><span class="token punctuation">]</span><span class="token punctuation">}</span>

<span class="token comment" spellcheck="true"># 3 获取文本内容</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>p<span class="token punctuation">.</span>string <span class="token comment" spellcheck="true"># 当p下只有文本取到，否则为None</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>p<span class="token punctuation">.</span>strings <span class="token comment" spellcheck="true"># 获取全部文本得到一个生成器对象</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>p<span class="token punctuation">.</span>text <span class="token comment" spellcheck="true"># 取到标签下所有的文本内容拼接起来</span>

<span class="token comment" spellcheck="true"># 4 嵌套选择</span>
<span class="token comment" spellcheck="true"># 可以让选择更加准确</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>p<span class="token punctuation">.</span>b<span class="token punctuation">.</span>string

<span class="token comment" spellcheck="true"># 5 子节点、子孙节点</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>p<span class="token punctuation">.</span>contents <span class="token comment" spellcheck="true"># 获得p内所有子节点</span>
<span class="token punctuation">[</span><span class="token string">'hello'</span><span class="token punctuation">,</span> <span class="token operator">&lt;</span>b <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"boldest"</span> id<span class="token operator">=</span><span class="token string">"bbb"</span><span class="token operator">></span>The Dormouse<span class="token string">'s story&lt;/b>, '</span>\n'<span class="token punctuation">]</span>

res <span class="token operator">=</span> soup<span class="token punctuation">.</span>p<span class="token punctuation">.</span>children <span class="token comment" spellcheck="true"># 得到一个迭代器，包含p内所有子节点</span>
<span class="token triple-quoted-string string">'''
子节点
hello
&lt;b class="boldest" id="bbb">The Dormouse's story&lt;/b>
'''</span>

res <span class="token operator">=</span> soup<span class="token punctuation">.</span>p<span class="token punctuation">.</span>descendants <span class="token comment" spellcheck="true"># 得到一个生成器，包含p内所有子孙节点</span>
<span class="token triple-quoted-string string">'''
子孙节点
hello
&lt;b class="boldest" id="bbb">The Dormouse's story&lt;/b>
The Dormouse's story
'''</span>

<span class="token comment" spellcheck="true"># 6 父节点、祖节点</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>p<span class="token punctuation">.</span>parent <span class="token comment" spellcheck="true"># 获得p的父节点</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>p<span class="token punctuation">.</span>parents <span class="token comment" spellcheck="true"># 获得p的父节点，和父节点的父节点。。。的生成器</span>

<span class="token comment" spellcheck="true"># 7 兄弟节点</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>p<span class="token punctuation">.</span>next_sibling <span class="token comment" spellcheck="true"># 下一个兄弟</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>p<span class="token punctuation">.</span>next_previous_sibling <span class="token comment" spellcheck="true"># 上一个兄弟</span></code></pre>
<h4 id="3-搜索文档树"><a href="#3-搜索文档树" class="headerlink" title="3 搜索文档树"></a>3 搜索文档树</h4><h5 id="3-1-五种过滤器"><a href="#3-1-五种过滤器" class="headerlink" title="3.1 五种过滤器"></a>3.1 五种过滤器</h5><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true"># 1 字符串</span>
<span class="token comment" spellcheck="true"># 如果有多个取第一个</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'b'</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true"># 获取全部，只有一个也是放在列表中</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token string">'b'</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 2 正则表达式</span>
<span class="token comment" spellcheck="true"># 获得所有以b开头的标签：body，b</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span>re<span class="token punctuation">.</span>compile<span class="token punctuation">(</span><span class="token string">'^b'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 3 列表</span>
<span class="token comment" spellcheck="true"># a标签，b标签都会取</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span><span class="token string">'b'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 4 布尔</span>
<span class="token comment" spellcheck="true"># 会查找到所有标签，不包括文本节点</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token boolean">True</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 5 自定义过滤器</span>
<span class="token comment" spellcheck="true"># 会把整个页面传给tag</span>
<span class="token keyword">def</span> <span class="token function">has_class_not_id</span><span class="token punctuation">(</span>tag<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> tag<span class="token punctuation">.</span>attrs<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'class'</span><span class="token punctuation">)</span> <span class="token operator">and</span> <span class="token operator">not</span> tag<span class="token punctuation">.</span>attrs<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'id'</span><span class="token punctuation">)</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span>has_class_not_id<span class="token punctuation">)</span>
</code></pre>
<h5 id="3-2-find-all"><a href="#3-2-find-all" class="headerlink" title="3.2 find_all()"></a>3.2 find_all()</h5><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true"># name：可以放任意类型的过滤器</span>

<span class="token comment" spellcheck="true"># keyword：放键值对</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span>id <span class="token operator">=</span> re<span class="token punctuation">.</span>compile<span class="token punctuation">(</span><span class="token string">'^my'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 如果有特殊参数不能通过k，v来表示</span>
<span class="token comment" spellcheck="true"># 比如data-foo = value</span>
soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span>attrs<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">"data-foo"</span><span class="token punctuation">:</span><span class="token string">"value"</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 按类名的时候，关键字是class_</span>
<span class="token comment" spellcheck="true"># 查找由sister和sss类的a标签</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span>class_<span class="token operator">=</span><span class="token string">'sister sss'</span><span class="token punctuation">)</span> 

<span class="token comment" spellcheck="true"># limit参数</span>
<span class="token comment" spellcheck="true"># 找前连个a标签</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span>limit<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span> 

<span class="token comment" spellcheck="true"># recursive = False 会查找tag下的直接的子节点</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>html<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span>recursive <span class="token operator">=</span> <span class="token boolean">False</span><span class="token punctuation">)</span></code></pre>
<h5 id="3-3-css选择器"><a href="#3-3-css选择器" class="headerlink" title="3.3 css选择器"></a>3.3 css选择器</h5><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true"># 选择器</span>

<span class="token comment" spellcheck="true"># 类</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">'.sister'</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true"># 含有sister类下的b标签</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">'.title b'</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># id</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">'#sister'</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 获取属性</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">'.title b'</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>attrs

<span class="token comment" spellcheck="true"># 获取内容</span>
res <span class="token operator">=</span> soup<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">'.title b'</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>text</code></pre>
</div><div id="reward-container"><span class="hty-icon-button button-glow" id="reward-button" title="打赏" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === &quot;none&quot;) ? &quot;block&quot; : &quot;none&quot;;"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-hand-coin-line"></use></svg></span><div id="reward-comment">点我就给你看点有意思的</div><div id="qr" style="display:none;"><div style="display:inline-block"><a target="_blank" rel="noopener" href="https://gitee.com/huang_zhao/hz/raw/master/hz/alipay.png"><img loading="lazy" src="https://gitee.com/huang_zhao/hz/raw/master/hz/alipay.png" alt="支付宝" title="支付宝"></a><div><span style="color:#00A3EE"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-alipay-line"></use></svg></span></div></div><div style="display:inline-block"><a target="_blank" rel="noopener" href="https://gitee.com/huang_zhao/hz/raw/master/hz/qqpay.jpg"><img loading="lazy" src="https://gitee.com/huang_zhao/hz/raw/master/hz/qqpay.jpg" alt="QQ 支付" title="QQ 支付"></a><div><span style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></span></div></div><div style="display:inline-block"><a target="_blank" rel="noopener" href="https://gitee.com/huang_zhao/hz/raw/master/hz/wxpay.jpg"><img loading="lazy" src="https://gitee.com/huang_zhao/hz/raw/master/hz/wxpay.jpg" alt="微信支付" title="微信支付"></a><div><span style="color:#2DC100"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-wechat-pay-line"></use></svg></span></div></div></div></div><ul class="post-copyright"><li class="post-copyright-author"><strong>本文作者：</strong>爱肖彤真是太好了</li><li class="post-copyright-link"><strong>本文链接：</strong><a href="http://huang_zhao.gitee.io/task/2020/04/21/python/%E7%88%AC%E8%99%AB/BeautifulSoup%E6%A8%A1%E5%9D%97%E7%9A%84%E4%BD%BF%E7%94%A8/" title="BeautifulSoup模块的使用">http://huang_zhao.gitee.io/task/2020/04/21/python/%E7%88%AC%E8%99%AB/BeautifulSoup%E6%A8%A1%E5%9D%97%E7%9A%84%E4%BD%BF%E7%94%A8/</a></li><li class="post-copyright-license"><strong>版权声明：</strong>本博客所有文章除特别声明外，均默认采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank" rel="noopener" title="CC BY-NC-SA 4.0 "><svg class="icon"><use xlink:href="#icon-creative-commons-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-by-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-nc-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-sa-line"></use></svg></a> 许可协议。</li></ul></section></article><div class="post-nav"><div class="post-nav-item"><a class="post-nav-prev" href="/task/2020/04/22/python/%E7%88%AC%E8%99%AB/scrapy%E7%88%AC%E8%99%AB%E6%A1%86%E6%9E%B6/" rel="prev" title="Scrapy框架"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-left-s-line"></use></svg><span class="post-nav-text">Scrapy框架</span></a></div><div class="post-nav-item"><a class="post-nav-next" href="/task/2020/04/21/%E6%95%B0%E6%8D%AE%E5%BA%93/redis/Redis%E6%8C%81%E4%B9%85%E5%8C%96%EF%BC%8C%E4%B8%BB%E4%BB%8E%EF%BC%8C%E5%93%A8%E5%85%B5%EF%BC%8C%E9%9B%86%E7%BE%A4/" rel="next" title="Redis持久化，主从，哨兵，集群"><span class="post-nav-text">Redis持久化，主从，哨兵，集群</span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-right-s-line"></use></svg></a></div></div></div></main><footer class="sidebar-translate" id="footer"><div class="copyright"><span>&copy; 2019 – 2022 </span><span class="with-love" id="animate"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-cloud-line"></use></svg></span><span class="author"> 爱肖彤真是太好了</span></div><div class="powered"><span>由 <a href="https://hexo.io" target="_blank" rel="noopener">Hexo</a> 驱动 v5.1.1</span><span class="footer-separator">|</span><span>主题 - <a rel="noopener" href="https://github.com/YunYouJun/hexo-theme-yun" target="_blank"><span>Yun</span></a> v0.9.7</span></div><div class="live_time"><span>本博客已萌萌哒地运行</span><span id="display_live_time"></span><span class="moe-text">(●'◡'●)</span><script>function blog_live_time() {
  window.setTimeout(blog_live_time, 1000);
  const start = new Date('2019-04-12T00:00:00');
  const now = new Date();
  const timeDiff = (now.getTime() - start.getTime());
  const msPerMinute = 60 * 1000;
  const msPerHour = 60 * msPerMinute;
  const msPerDay = 24 * msPerHour;
  const passDay = Math.floor(timeDiff / msPerDay);
  const passHour = Math.floor((timeDiff % msPerDay) / 60 / 60 / 1000);
  const passMinute = Math.floor((timeDiff % msPerHour) / 60 / 1000);
  const passSecond = Math.floor((timeDiff % msPerMinute) / 1000);
  display_live_time.innerHTML = " " + passDay + " 天 " + passHour + " 小时 " + passMinute + " 分 " + passSecond + " 秒";
}
blog_live_time();
</script></div><div id="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_site_uv" title="总访客量"><span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-user-line"></use></svg></span><span id="busuanzi_value_site_uv"></span></span><span class="footer-separator">|</span><span id="busuanzi_container_site_pv" title="总访问量"><span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-eye-line"></use></svg></span><span id="busuanzi_value_site_pv"></span></span></div></footer><a class="hty-icon-button" id="goUp" aria-label="back-to-top" href="#"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-up-s-line"></use></svg><svg class="progress-circle-container" viewBox="0 0 100 100"><circle class="progress-circle" id="progressCircle" cx="50" cy="50" r="48" fill="none" stroke="#0078E7" stroke-width="2" stroke-linecap="round"></circle></svg></a><a class="popup-trigger hty-icon-button icon-search" id="search" href="javascript:;" title="搜索"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-search-line"></use></svg></span></a><script>window.addEventListener("DOMContentLoaded", () => {
  // Handle and trigger popup window
  document.querySelector(".popup-trigger").addEventListener("click", () => {
    document.querySelector(".popup").classList.add("show");
    setTimeout(() => {
      document.querySelector(".search-input").focus();
    }, 100);
  });

  // Monitor main search box
  const onPopupClose = () => {
    document.querySelector(".popup").classList.remove("show");
  };

  document.querySelector(".popup-btn-close").addEventListener("click", () => {
    onPopupClose();
  });

  window.addEventListener("keyup", event => {
    if (event.key === "Escape") {
      onPopupClose();
    }
  });
});
</script><script src="/task/js/search/local-search.js" defer></script><div class="popup search-popup"><div class="search-header"><span class="popup-btn-close close-icon hty-icon-button"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-close-line"></use></svg></span></div><div class="search-input-container"><input class="search-input" id="local-search-input" type="text" placeholder="搜索..." value=""></div><div id="local-search-result"></div></div></div><script defer src="/task/js/utils.js"></script><script defer src="/task/js/hexo-theme-yun.js"></script></body></html>